import { apiExpenseExport } from '@api/cost/cost'
import { exportBlob, getFileNameByResponse } from '@/utils/util'
import moment from 'moment'

/**
 * 费用导出 mixin
 */
export default {
  data () {
    return {
      exportLoading: false
    }
  },
  methods: {
    /**
     * 处理导出
     * @param params
     * @param {string} params.orgId 研究中心 必传
     */
    handleExport (params) {
      if (!params.orgId) {
        return this.$message.error(this.$t('all.autotext825'))
      }
      this.exportLoading = true
      apiExpenseExport(params).then(res => {
        const fileName = this.getExportFileName(res)
        exportBlob(fileName, res.data)
      }).finally(() => {
        this.exportLoading = false
      })
    },
    /**
     * 获取导出文件名
     */
    getExportFileName ({ headers = {} }) {
      const date = moment().format('YYYY-MM-DD')
      const filename = getFileNameByResponse(headers)
      return filename || `${this.$t('all.autotext246')} ${date}`
    }
  }
}
